import {NButton, SelectOption} from 'naive-ui'
import {h} from "vue"
import url from '@/static/config'
import {msm_order_sales, sms_order_confirm} from '@/api/sms'

export type OrderQuery = {
  order_status: string
}
export type OrderTableData = {
  date: string
  order_no: string
  order_product_total: number
  order_status: string
  product_name: string
  product_pic: string
  username: string
}

export const orderColumns = (table) => [
  {
    title: "订单编号",
    align: "center",
    key: "order_no",
    render: (r) => {
      const arr = r.order_no.split("-")
      let str = ""
      for (const item of arr) {
        str += item
      }
      return str
    }
  },
  {
    title: "商品图片",
    align: "center",
    key: 'product_pic',
    width: 100,
    render: (r:OrderTableData) => {
      return h("img", {
        src: url + "static/product/" + r.product_pic,
        width: 80,
        height: 80
      })
    }
  },
  {
    title: "商品名称",
    align: "center",
    key: "product_name"
  },
  {
    title: "订单状态",
    align: "center",
    width: 100,
    key: "order_status",
    render: (r:OrderTableData) => {
      switch (r.order_status) {
        case "0":
          return "未付款"
        case "1":
          return '已付款'
        case "2":
          return '已发货'
        case "3":
          return '已签收'
        case "4":
          return '确认收货'
        case "7":
          return "申请退货"
        case "5":
          return "退货中"
        case "6":
          return "已退货"
      }
    }
  },
  {
    title: "价格",
    align: "center",
    width: 100,
    render: (r:OrderTableData) => {
      return r.order_product_total.toFixed(2)
    }
  },
  {
    title: "时间",
    align: "center",
    key: "date",
    width: 200,
  },
  {
    title: "收货人",
    key: "username",
    align: "center",
    width: 100
  },
  {
    title: "操作",
    align: "center",
    width: 200,
    render: (r:OrderTableData) => {
      if (r.order_status === '1') {
        return h(NButton,{
          type: "info",
          onClick: () => {
            // console.log(r.order_no)
            sms_order_confirm({
              order_no: r.order_no
            }).then(res => {
              if (res.code === 200) {
                table()
              }
            })
          }
        }, "确认发货")
      }
      if (r.order_status === "7") {
        return h(NButton, {
          type: "info",
          onClick: () => {
            console.log(r)
            msm_order_sales(r).then(res => {
              if (res.code === 200) {
                table()
              }
            })
          }
        }, "确认退货")
      }
    }
  },
]
export const orderSelectOptions:SelectOption = [
  {
    value: '',
    label: '全部'
  },
  {
    value: '0',
    label: '未付款'
  },
  {
    value: '1',
    label: '已付款'
  },
  {
    value: '2',
    label: '已发货'
  },
  {
    value: '3',
    label: '已签收'
  },
  {
    value: '4',
    label: '确认收货'
  },
]
